package cn.sccl.dirms.topo.discovery.service.impl;

import cn.sccl.common.dao.BaseDAO;
import cn.sccl.common.database.DataBase;
import cn.sccl.common.service.impl.BaseManagerImpl;
import cn.sccl.dirms.topo.discovery.dao.DiscoverRuleDAO;
import cn.sccl.dirms.topo.discovery.model.DiscoverReTaskRule;
import cn.sccl.dirms.topo.discovery.model.DiscoverRule;
import cn.sccl.dirms.topo.discovery.model.DiscoverRuleExtends;
import cn.sccl.dirms.topo.discovery.service.DiscoverRuleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
* 自动发现规则 的Manager实现类
* Created by _minus on 2017-09-22.
*/
@Service
public class DiscoverRuleManagerImpl extends BaseManagerImpl<DiscoverRule> implements DiscoverRuleManager{

    @Autowired
    private DiscoverRuleDAO discoverRuleDAO;

    public DiscoverRuleDAO getDiscoverRuleDAO(){
        return discoverRuleDAO;
    }

    @Override
    public BaseDAO<DiscoverRule> getBaseDAO(){
        return getDiscoverRuleDAO();
    }


    @Autowired
    DataBase dataBase;

    @Override
    public DataBase getDataBase(){return this.dataBase ;}

    @Override
    public List<DiscoverRuleExtends> queryDiscoverRuleExtendsByTaskId(String id) {
        StringBuffer sql=new StringBuffer();
        sql.append(
                "SELECT R.*, T.ID AS taskid,RT.id as retaskruleid\n" +
                        "  FROM T_TOPO_DISCOVER_RULE         R,\n" +
                        "       T_TOPO_DISCOVER_TASK         T,\n" +
                        "       T_TOPO_DISCOVER_RE_TASK_RULE RT\n" +
                        " WHERE T.DELETE_FLAG = 0\n" +
                        "   AND R.DELETE_FLAG = 0\n" +
                        "   AND RT.DELETE_FLAG = 0\n" +
                        "   AND RT.TASK_ID = T.ID\n" +
                        "   AND RT.DISCOVER_RULE_ID = R.ID\n"+
                        "   AND t.id = ? "

        );
        List params = new ArrayList();
        params.add(id);
        return getDataBase().query4Model(sql.toString(),DiscoverRuleExtends.class,params.toArray());
    }

    @Override
    public List<DiscoverRule> queryAvailableDiscoverRuleByTaskId(String id) {
        String sql =
                "select r.* from t_topo_discover_rule r where r.delete_flag = 0 and NOT EXISTS\n" +
                        "\n" +
                        "(SELECT 1 FROM t_topo_discover_re_task_rule rt WHERE rt.delete_flag = 0 AND rt.discover_rule_id = r.id)";

        List params = new ArrayList();

        return getDataBase().query4Model(sql,DiscoverRule.class,params.toArray());
    }

    @Override
    public DiscoverRuleExtends findDiscoverRuleExtendsByRelationTaskRule(DiscoverReTaskRule discoverReTaskRule) {
        StringBuffer sql =new StringBuffer(
                "SELECT R.*,rt.id AS retaskruleid ,t.id AS taskid\n" +
                        "  FROM T_TOPO_DISCOVER_RULE         R,\n" +
                        "       T_TOPO_DISCOVER_RE_TASK_RULE RT,\n" +
                        "       T_TOPO_DISCOVER_TASK         T\n" +
                        " WHERE R.DELETE_FLAG = 0\n" +
                        "   AND RT.DELETE_FLAG = 0 AND T.DELETE_FLAG = 0\n" +
                        "   AND rt.task_id =t.ID AND rt.discover_rule_id = r.id");
        List params = new ArrayList();

        if (!StringUtils.isEmpty(discoverReTaskRule.getId())){
            sql.append(" and rt.id = ? ");
            params.add(discoverReTaskRule.getId());
        }

        if (!StringUtils.isEmpty(discoverReTaskRule.getDiscoverRuleId())){
            sql.append(" and rt.discover_rule_id = ? ");
            params.add(discoverReTaskRule.getDiscoverRuleId());
        }

        if (!StringUtils.isEmpty(discoverReTaskRule.getTaskId())){
            sql.append(" and rt.task_id = ? ");
            params.add(discoverReTaskRule.getTaskId());
        }
        List<DiscoverRuleExtends> discoverRuleExtends =getDataBase().query4Model(sql.toString(),DiscoverRuleExtends.class,params.toArray());
        if (discoverRuleExtends != null && discoverRuleExtends.size() ==1) {
            return discoverRuleExtends.get(0);
        }
        return null;
    }
}